package app

import (
	"context"

	"charging_pile/common/ctxdata"
	"charging_pile/common/xerr"
	"charging_pile/models"
	"charging_pile/user/internal/svc"
	"charging_pile/user/internal/types"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type BindCarLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewBindCarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindCarLogic {
	return &BindCarLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *BindCarLogic) BindCar(req *types.BindCarReq) (resp *types.BindCarResp, err error) {
	uid := ctxdata.GetUidFromCtx(l.ctx)
	_, err = l.svcCtx.Models.Car.Insert(l.ctx, nil, &models.Car{
		UserId:  uid,
		CarType: req.CarType,
		CarNo:   req.CarNum,
	})
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "绑定车辆,数据库出错")
	}

	return &types.BindCarResp{}, nil
}
